package wbbs.service;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Singleton
public class LoginService {

    @Inject
    Provider<HttpServletRequest> requestProvider;

    public String getLoginUserId() {
        HttpServletRequest request = requestProvider.get();
        HttpSession session = request.getSession(false);
        if (session == null) return null;
        return (String) session.getAttribute("loginUser");
    }

    public void setLoginUserId(String id) {
        HttpServletRequest request = requestProvider.get();
        if (id == null) {
            HttpSession session = request.getSession(false);
            if (session == null) return;
            session.invalidate();
        } else {
            request.getSession().setAttribute("loginUser", id);
        }
    }
}
